home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- ident = '$Id: XMLSchema.py 1434 2007-11-01 22:42:47Z boverhof $'
- import types
- import weakref
- import sys
- import warnings
- from Namespaces import SCHEMA, XMLNS, SOAP
- from Utility import DOM, DOMException, Collection, SplitQName, basejoin
- from StringIO import StringIO
-
- try:
- from threading import RLock
- except ImportError:
-
- class RLock:
-
- def acquire():
- pass
-
-
- def release():
- pass
-
-
-
- TYPES = 'types'
- ATTRIBUTE_GROUPS = 'attr_groups'
- ATTRIBUTES = 'attr_decl'
- ELEMENTS = 'elements'
- MODEL_GROUPS = 'model_groups'
- BUILT_IN_NAMESPACES = [
- SOAP.ENC] + SCHEMA.XSD_LIST
-
- def GetSchema(component):
- parent = component
- while not isinstance(parent, XMLSchema):
- parent = parent._parent()
- return parent
-
-
- class SchemaReader:
- namespaceToSchema = { }
-
- def __init__(self, domReader = None, base_url = None):
- self._SchemaReader__base_url = base_url
- self._SchemaReader__readerClass = domReader
- if not self._SchemaReader__readerClass:
- self._SchemaReader__readerClass = DOMAdapter
-
- self._includes = { }
- self._imports = { }
-
-
- def __setImports(self, schema):
- for ns, val in schema.imports.items():
- if self._imports.has_key(ns):
- schema.addImportSchema(self._imports[ns])
- continue
-
-
-
- def __setIncludes(self, schema):
- for schemaLocation, val in schema.includes.items():
- if self._includes.has_key(schemaLocation):
- schema.addIncludeSchema(schemaLocation, self._imports[schemaLocation])
- continue
-
-
-
- def addSchemaByLocation(self, location, schema):
- self._includes[location] = schema
-
-
- def addSchemaByNamespace(self, schema):
- self._imports[schema.targetNamespace] = schema
-
-
- def loadFromNode(self, parent, element):
- reader = self._SchemaReader__readerClass(element)
- schema = XMLSchema(parent)
- schema.wsdl = parent
- schema.setBaseUrl(self._SchemaReader__base_url)
- schema.load(reader)
- return schema
-
-
- def loadFromStream(self, file, url = None):
- reader = self._SchemaReader__readerClass()
- reader.loadDocument(file)
- schema = XMLSchema()
- if url is not None:
- schema.setBaseUrl(url)
-
- schema.load(reader)
- self._SchemaReader__setIncludes(schema)
- self._SchemaReader__setImports(schema)
- return schema
-
-
- def loadFromString(self, data):
- return self.loadFromStream(StringIO(data))
-
-
- def loadFromURL(self, url, schema = None):
- reader = self._SchemaReader__readerClass()
- if self._SchemaReader__base_url:
- url = basejoin(self._SchemaReader__base_url, url)
-
- reader.loadFromURL(url)
- if not schema:
- pass
- schema = XMLSchema()
- schema.setBaseUrl(url)
- schema.load(reader)
- self._SchemaReader__setIncludes(schema)
- self._SchemaReader__setImports(schema)
- return schema
-
-
- def loadFromFile(self, filename):
- if self._SchemaReader__base_url:
- filename = basejoin(self._SchemaReader__base_url, filename)
-
- file = open(filename, 'rb')
-
- try:
- schema = self.loadFromStream(file, filename)
- finally:
- file.close()
-
- return schema
-
-
-
- class SchemaError(Exception):
- pass
-
-
- class NoSchemaLocationWarning(Exception):
- pass
-
-
- class DOMAdapterInterface:
-
- def hasattr(self, attr, ns = None):
- raise NotImplementedError, 'adapter method not implemented'
-
-
- def getContentList(self, *contents):
- raise NotImplementedError, 'adapter method not implemented'
-
-
- def setAttributeDictionary(self, attributes):
- raise NotImplementedError, 'adapter method not implemented'
-
-
- def getAttributeDictionary(self):
- raise NotImplementedError, 'adapter method not implemented'
-
-
- def getNamespace(self, prefix):
- raise NotImplementedError, 'adapter method not implemented'
-
-
- def getTagName(self):
- raise NotImplementedError, 'adapter method not implemented'
-
-
- def getParentNode(self):
- raise NotImplementedError, 'adapter method not implemented'
-
-
- def loadDocument(self, file):
- raise NotImplementedError, 'adapter method not implemented'
-
-
- def loadFromURL(self, url):
- raise NotImplementedError, 'adapter method not implemented'
-
-
-
- class DOMAdapter(DOMAdapterInterface):
-
- def __init__(self, node = None):
- if hasattr(node, 'documentElement'):
- self._DOMAdapter__node = node.documentElement
- else:
- self._DOMAdapter__node = node
- self._DOMAdapter__attributes = None
-
-
- def getNode(self):
- return self._DOMAdapter__node
-
-
- def hasattr(self, attr, ns = None):
- if not self._DOMAdapter__attributes:
- self.setAttributeDictionary()
-
- if ns:
- return self._DOMAdapter__attributes.get(ns, { }).has_key(attr)
-
- return self._DOMAdapter__attributes.has_key(attr)
-
-
- def getContentList(self, *contents):
- nodes = []
- ELEMENT_NODE = self._DOMAdapter__node.ELEMENT_NODE
- for child in DOM.getElements(self._DOMAdapter__node, None):
- if child.nodeType == ELEMENT_NODE and SplitQName(child.tagName)[1] in contents:
- nodes.append(child)
- continue
-
- return map(self.__class__, nodes)
-
-
- def setAttributeDictionary(self):
- self._DOMAdapter__attributes = { }
- for v in self._DOMAdapter__node._attrs.values():
- self._DOMAdapter__attributes[v.nodeName] = v.nodeValue
-
-
-
- def getAttributeDictionary(self):
- if not self._DOMAdapter__attributes:
- self.setAttributeDictionary()
-
- return self._DOMAdapter__attributes
-
-
- def getTagName(self):
- return self._DOMAdapter__node.tagName
-
-
- def getParentNode(self):
- if self._DOMAdapter__node.parentNode.nodeType == self._DOMAdapter__node.ELEMENT_NODE:
- return DOMAdapter(self._DOMAdapter__node.parentNode)
-
-
-
- def getNamespace(self, prefix):
- namespace = None
- if prefix == 'xmlns':
- namespace = DOM.findDefaultNS(prefix, self._DOMAdapter__node)
- else:
-
- try:
- namespace = DOM.findNamespaceURI(prefix, self._DOMAdapter__node)
- except DOMException:
- ex = None
- if prefix != 'xml':
- raise SchemaError, '%s namespace not declared for %s' % (prefix, self._DOMAdapter__node._get_tagName())
-
- namespace = XMLNS.XML
-
- return namespace
-
-
- def loadDocument(self, file):
- self._DOMAdapter__node = DOM.loadDocument(file)
- if hasattr(self._DOMAdapter__node, 'documentElement'):
- self._DOMAdapter__node = self._DOMAdapter__node.documentElement
-
-
-
- def loadFromURL(self, url):
- self._DOMAdapter__node = DOM.loadFromURL(url)
- if hasattr(self._DOMAdapter__node, 'documentElement'):
- self._DOMAdapter__node = self._DOMAdapter__node.documentElement
-
-
-
-
- class XMLBase:
- tag = None
- __indent = 0
- __rlock = RLock()
-
- def __str__(self):
- XMLBase._XMLBase__rlock.acquire()
- XMLBase._XMLBase__indent += 1
- tmp = '<' + str(self.__class__) + '>\n'
- for k, v in self.__dict__.items():
- tmp += '%s* %s = %s\n' % (XMLBase._XMLBase__indent * ' ', k, v)
-
- XMLBase._XMLBase__indent -= 1
- XMLBase._XMLBase__rlock.release()
- return tmp
-
-
-
- class DefinitionMarker:
- pass
-
-
- class DeclarationMarker:
- pass
-
-
- class AttributeMarker:
- pass
-
-
- class AttributeGroupMarker:
- pass
-
-
- class WildCardMarker:
- pass
-
-
- class ElementMarker:
- pass
-
-
- class ReferenceMarker:
- pass
-
-
- class ModelGroupMarker:
- pass
-
-
- class AllMarker(ModelGroupMarker):
- pass
-
-
- class ChoiceMarker(ModelGroupMarker):
- pass
-
-
- class SequenceMarker(ModelGroupMarker):
- pass
-
-
- class ExtensionMarker:
- pass
-
-
- class RestrictionMarker:
- facets = [
- 'enumeration',
- 'length',
- 'maxExclusive',
- 'maxInclusive',
- 'maxLength',
- 'minExclusive',
- 'minInclusive',
- 'minLength',
- 'pattern',
- 'fractionDigits',
- 'totalDigits',
- 'whiteSpace']
-
-
- class SimpleMarker:
- pass
-
-
- class ListMarker:
- pass
-
-
- class UnionMarker:
- pass
-
-
- class ComplexMarker:
- pass
-
-
- class LocalMarker:
- pass
-
-
- class MarkerInterface:
-
- def isDefinition(self):
- return isinstance(self, DefinitionMarker)
-
-
- def isDeclaration(self):
- return isinstance(self, DeclarationMarker)
-
-
- def isAttribute(self):
- return isinstance(self, AttributeMarker)
-
-
- def isAttributeGroup(self):
- return isinstance(self, AttributeGroupMarker)
-
-
- def isElement(self):
- return isinstance(self, ElementMarker)
-
-
- def isReference(self):
- return isinstance(self, ReferenceMarker)
-
-
- def isWildCard(self):
- return isinstance(self, WildCardMarker)
-
-
- def isModelGroup(self):
- return isinstance(self, ModelGroupMarker)
-
-
- def isAll(self):
- return isinstance(self, AllMarker)
-
-
- def isChoice(self):
- return isinstance(self, ChoiceMarker)
-
-
- def isSequence(self):
- return isinstance(self, SequenceMarker)
-
-
- def isExtension(self):
- return isinstance(self, ExtensionMarker)
-
-
- def isRestriction(self):
- return isinstance(self, RestrictionMarker)
-
-
- def isSimple(self):
- return isinstance(self, SimpleMarker)
-
-
- def isComplex(self):
- return isinstance(self, ComplexMarker)
-
-
- def isLocal(self):
- return isinstance(self, LocalMarker)
-
-
- def isList(self):
- return isinstance(self, ListMarker)
-
-
- def isUnion(self):
- return isinstance(self, UnionMarker)
-
-
-
- class XMLSchemaComponent(XMLBase, MarkerInterface):
- required = []
- attributes = { }
- contents = { }
- xmlns_key = ''
- xmlns = 'xmlns'
- xml = 'xml'
-
- def __init__(self, parent = None):
- self.attributes = None
- self._parent = parent
- if self._parent:
- self._parent = weakref.ref(parent)
-
- if not (self.__class__ == XMLSchemaComponent):
- if type(self.__class__.required) == type(XMLSchemaComponent.required) and type(self.__class__.attributes) == type(XMLSchemaComponent.attributes):
- pass
- if not (type(self.__class__.contents) == type(XMLSchemaComponent.contents)):
- raise RuntimeError, 'Bad type for a class variable in %s' % self.__class__
-
-
-
- def getItemTrace(self):
- (item, path, name, ref) = (self, [], 'name', 'ref')
- while not isinstance(item, XMLSchema) and not isinstance(item, WSDLToolsAdapter):
- attr = item.getAttribute(name)
- if not attr:
- attr = item.getAttribute(ref)
- if not attr:
- path.append('<%s>' % item.tag)
- else:
- path.append('<%s ref="%s">' % (item.tag, attr))
- else:
- path.append('<%s name="%s">' % (item.tag, attr))
- item = item._parent()
-
- try:
- tns = item.getTargetNamespace()
- except:
- tns = ''
-
- path.append('<%s targetNamespace="%s">' % (item.tag, tns))
- path.reverse()
- return ''.join(path)
-
-
- def getTargetNamespace(self):
- parent = self
- targetNamespace = 'targetNamespace'
- tns = self.attributes.get(targetNamespace)
- while not tns and parent and parent._parent is not None:
- parent = parent._parent()
- tns = parent.attributes.get(targetNamespace)
- if not tns:
- pass
- return ''
-
-
- def getAttributeDeclaration(self, attribute):
- return self.getQNameAttribute(ATTRIBUTES, attribute)
-
-
- def getAttributeGroup(self, attribute):
- return self.getQNameAttribute(ATTRIBUTE_GROUPS, attribute)
-
-
- def getTypeDefinition(self, attribute):
- return self.getQNameAttribute(TYPES, attribute)
-
-
- def getElementDeclaration(self, attribute):
- return self.getQNameAttribute(ELEMENTS, attribute)
-
-
- def getModelGroup(self, attribute):
- return self.getQNameAttribute(MODEL_GROUPS, attribute)
-
-
- def getQNameAttribute(self, collection, attribute):
- tdc = self.getAttributeQName(attribute)
- if not tdc:
- return None
-
- obj = self.getSchemaItem(collection, tdc.getTargetNamespace(), tdc.getName())
- if obj:
- return obj
-
-
-
- def getSchemaItem(self, collection, namespace, name):
- parent = GetSchema(self)
- if parent.targetNamespace == namespace:
-
- try:
- obj = getattr(parent, collection)[name]
- except KeyError:
- ex = None
- raise KeyError, 'targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name)
-
- return obj
-
- if not parent.imports.has_key(namespace):
- if namespace in BUILT_IN_NAMESPACES:
- return None
-
- raise SchemaError, 'schema "%s" does not import namespace "%s"' % (parent.targetNamespace, namespace)
-
- schema = parent.imports[namespace]
- if not isinstance(schema, XMLSchema):
- schema = schema.getSchema()
- if schema is not None:
- parent.imports[namespace] = schema
-
-
- if schema is None:
- if namespace in BUILT_IN_NAMESPACES:
- return None
-
- raise SchemaError, 'no schema instance for imported namespace (%s).' % namespace
-
- if not isinstance(schema, XMLSchema):
- raise TypeError, 'expecting XMLSchema instance not "%r"' % schema
-
-
- try:
- obj = getattr(schema, collection)[name]
- except KeyError:
- ex = None
- raise KeyError, 'targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name)
-
- return obj
-
-
- def getXMLNS(self, prefix = None):
- if prefix == XMLSchemaComponent.xml:
- return XMLNS.XML
-
- parent = self
- if not prefix:
- pass
- ns = self.attributes[XMLSchemaComponent.xmlns].get(XMLSchemaComponent.xmlns_key)
- while not ns:
- parent = parent._parent()
- if not prefix:
- pass
- ns = parent.attributes[XMLSchemaComponent.xmlns].get(XMLSchemaComponent.xmlns_key)
- if not ns and isinstance(parent, WSDLToolsAdapter):
- if prefix is None:
- return ''
-
- raise SchemaError, 'unknown prefix %s' % prefix
- continue
- return ns
-
-
- def getAttribute(self, attribute):
- if type(attribute) in (list, tuple):
- if len(attribute) != 2:
- raise LookupError, 'To access attributes must use name or (namespace,name)'
-
- ns_dict = self.attributes.get(attribute[0])
- if ns_dict is None:
- return None
-
- return ns_dict.get(attribute[1])
-
- return self.attributes.get(attribute)
-
-
- def getAttributeQName(self, attribute):
- qname = self.getAttribute(attribute)
- if isinstance(qname, TypeDescriptionComponent) is True:
- return qname
-
- if qname is None:
- return None
-
- (prefix, ncname) = SplitQName(qname)
- namespace = self.getXMLNS(prefix)
- return TypeDescriptionComponent((namespace, ncname))
-
-
- def getAttributeName(self):
- return self.getAttribute('name')
-
-
- def setAttributes(self, node):
- self.attributes = {
- XMLSchemaComponent.xmlns: { } }
- for k, v in node.getAttributeDictionary().items():
- (prefix, value) = SplitQName(k)
- if value == XMLSchemaComponent.xmlns:
- if not prefix:
- pass
- self.attributes[value][XMLSchemaComponent.xmlns_key] = v
- continue
- if prefix:
- ns = node.getNamespace(prefix)
- if not ns:
- raise SchemaError, 'no namespace for attribute prefix %s' % prefix
-
- if not self.attributes.has_key(ns):
- self.attributes[ns] = { }
- elif self.attributes[ns].has_key(value):
- raise SchemaError, 'attribute %s declared multiple times in %s' % (value, ns)
-
- self.attributes[ns][value] = v
- continue
- if not self.attributes.has_key(value):
- self.attributes[value] = v
- continue
- raise SchemaError, 'attribute %s declared multiple times' % value
-
- if not isinstance(self, WSDLToolsAdapter):
- self._XMLSchemaComponent__checkAttributes()
-
- self._XMLSchemaComponent__setAttributeDefaults()
- for k in [
- 'type',
- 'element',
- 'base',
- 'ref',
- 'substitutionGroup',
- 'itemType']:
- if self.attributes.has_key(k):
- (prefix, value) = SplitQName(self.attributes.get(k))
- self.attributes[k] = TypeDescriptionComponent((self.getXMLNS(prefix), value))
- continue
-
- for k in [
- 'memberTypes']:
- if self.attributes.has_key(k):
- qnames = self.attributes[k]
- self.attributes[k] = []
- for qname in qnames.split():
- (prefix, value) = SplitQName(qname)
- self.attributes['memberTypes'].append(TypeDescriptionComponent((self.getXMLNS(prefix), value)))
-
-
-
-
- def getContents(self, node):
- return node.getContentList(*self.__class__.contents['xsd'])
-
-
- def __setAttributeDefaults(self):
- for k, v in self.__class__.attributes.items():
- if v is not None and self.attributes.has_key(k) is False:
- if isinstance(v, types.FunctionType):
- self.attributes[k] = v(self)
- else:
- self.attributes[k] = v
- isinstance(v, types.FunctionType)
-
-
-
- def __checkAttributes(self):
- for a in self.__class__.required:
- if not self.attributes.has_key(a):
- raise SchemaError, 'class instance %s, missing required attribute %s' % (self.__class__, a)
- continue
-
- for a, v in self.attributes.items():
- if type(v) is dict:
- continue
-
- if a in (XMLSchemaComponent.xmlns, XMLNS.XML):
- continue
-
- if a not in self.__class__.attributes.keys():
- if self.isAttribute():
- pass
- if not self.isReference():
- raise SchemaError, '%s, unknown attribute(%s,%s)' % (self.getItemTrace(), a, self.attributes[a])
- continue
-
-
-
-
- class WSDLToolsAdapter(XMLSchemaComponent):
- attributes = {
- 'name': None,
- 'targetNamespace': None }
- tag = 'definitions'
-
- def __init__(self, wsdl):
- XMLSchemaComponent.__init__(self, parent = wsdl)
- self.setAttributes(DOMAdapter(wsdl.document))
-
-
- def getImportSchemas(self):
- return self._parent().types
-
-
-
- class Notation(XMLSchemaComponent):
- required = [
- 'name',
- 'public']
- attributes = {
- 'id': None,
- 'name': None,
- 'public': None,
- 'system': None }
- contents = {
- 'xsd': 'annotation' }
- tag = 'notation'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class Annotation(XMLSchemaComponent):
- attributes = {
- 'id': None }
- contents = {
- 'xsd': ('documentation', 'appinfo') }
- tag = 'annotation'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'documentation':
- continue
- continue
- if component == 'appinfo':
- continue
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.content = tuple(content)
-
-
- class Documentation(XMLSchemaComponent):
- attributes = {
- 'source': None,
- 'xml:lang': None }
- contents = {
- 'xsd': ('mixed', 'any') }
- tag = 'documentation'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'mixed':
- continue
- continue
- if component == 'any':
- continue
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.content = tuple(content)
-
-
-
- class Appinfo(XMLSchemaComponent):
- attributes = {
- 'source': None,
- 'anyURI': None }
- contents = {
- 'xsd': ('mixed', 'any') }
- tag = 'appinfo'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'mixed':
- continue
- continue
- if component == 'any':
- continue
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.content = tuple(content)
-
-
-
-
- class XMLSchemaFake:
-
- def __init__(self, element):
- self.targetNamespace = DOM.getAttr(element, 'targetNamespace')
- self.element = element
-
-
-
- class XMLSchema(XMLSchemaComponent):
- attributes = {
- 'id': None,
- 'version': None,
- 'xml:lang': None,
- 'targetNamespace': None,
- 'attributeFormDefault': 'unqualified',
- 'elementFormDefault': 'unqualified',
- 'blockDefault': None,
- 'finalDefault': None }
- contents = {
- 'xsd': ('include', 'import', 'redefine', 'annotation', 'attribute', 'attributeGroup', 'complexType', 'element', 'group', 'notation', 'simpleType', 'annotation') }
- empty_namespace = ''
- tag = 'schema'
-
- def __init__(self, parent = None):
- self._XMLSchema__node = None
- self.targetNamespace = None
- XMLSchemaComponent.__init__(self, parent)
-
- f = lambda k: k.attributes['name']
-
- ns = lambda k: k.attributes['namespace']
-
- sl = lambda k: k.attributes['schemaLocation']
- self.includes = Collection(self, key = sl)
- self.imports = Collection(self, key = ns)
- self.elements = Collection(self, key = f)
- self.types = Collection(self, key = f)
- self.attr_decl = Collection(self, key = f)
- self.attr_groups = Collection(self, key = f)
- self.model_groups = Collection(self, key = f)
- self.notations = Collection(self, key = f)
- self._imported_schemas = { }
- self._included_schemas = { }
- self._base_url = None
-
-
- def getNode(self):
- return self._XMLSchema__node
-
-
- def addImportSchema(self, schema):
- if not isinstance(schema, XMLSchema):
- raise TypeError, 'expecting a Schema instance'
-
- if schema.targetNamespace != self.targetNamespace:
- self._imported_schemas[schema.targetNamespace] = schema
- else:
- raise SchemaError, 'import schema bad targetNamespace'
-
-
- def addIncludeSchema(self, schemaLocation, schema):
- if not isinstance(schema, XMLSchema):
- raise TypeError, 'expecting a Schema instance'
-
- if not (schema.targetNamespace) or schema.targetNamespace == self.targetNamespace:
- self._included_schemas[schemaLocation] = schema
- else:
- raise SchemaError, 'include schema bad targetNamespace'
-
-
- def setImportSchemas(self, schema_dict):
- self._imported_schemas = schema_dict
-
-
- def getImportSchemas(self):
- return self._imported_schemas
-
-
- def getSchemaNamespacesToImport(self):
- return tuple(self.includes.keys())
-
-
- def setIncludeSchemas(self, schema_dict):
- self._included_schemas = schema_dict
-
-
- def getIncludeSchemas(self):
- return self._included_schemas
-
-
- def getBaseUrl(self):
- return self._base_url
-
-
- def setBaseUrl(self, url):
- self._base_url = url
-
-
- def getElementFormDefault(self):
- return self.attributes.get('elementFormDefault')
-
-
- def isElementFormDefaultQualified(self):
- return self.attributes.get('elementFormDefault') == 'qualified'
-
-
- def getAttributeFormDefault(self):
- return self.attributes.get('attributeFormDefault')
-
-
- def getBlockDefault(self):
- return self.attributes.get('blockDefault')
-
-
- def getFinalDefault(self):
- return self.attributes.get('finalDefault')
-
-
- def load(self, node, location = None):
- self._XMLSchema__node = node
- pnode = node.getParentNode()
- if pnode:
- pname = SplitQName(pnode.getTagName())[1]
- if pname == 'types':
- attributes = { }
- self.setAttributes(pnode)
- attributes.update(self.attributes)
- self.setAttributes(node)
- for k, v in attributes['xmlns'].items():
- if not self.attributes['xmlns'].has_key(k):
- self.attributes['xmlns'][k] = v
- continue
-
- else:
- self.setAttributes(node)
- else:
- self.setAttributes(node)
- self.targetNamespace = self.getTargetNamespace()
- for childNode in self.getContents(node):
- component = SplitQName(childNode.getTagName())[1]
- if component == 'include':
- tp = self.__class__.Include(self)
- tp.fromDom(childNode)
- sl = tp.attributes['schemaLocation']
- schema = tp.getSchema()
- if not self.getIncludeSchemas().has_key(sl):
- self.addIncludeSchema(sl, schema)
-
- self.includes[sl] = tp
- pn = childNode.getParentNode().getNode()
- pn.removeChild(childNode.getNode())
- for child in schema.getNode().getNode().childNodes:
- pn.appendChild(child.cloneNode(1))
-
- for collection in [
- 'imports',
- 'elements',
- 'types',
- 'attr_decl',
- 'attr_groups',
- 'model_groups',
- 'notations']:
- for k, v in getattr(schema, collection).items():
- if not getattr(self, collection).has_key(k):
- v._parent = weakref.ref(self)
- getattr(self, collection)[k] = v
- continue
- warnings.warn('Not keeping schema component.')
-
-
- if component == 'import':
- slocd = SchemaReader.namespaceToSchema
- tp = self.__class__.Import(self)
- tp.fromDom(childNode)
- if not tp.getAttribute('namespace'):
- pass
- import_ns = self.__class__.empty_namespace
- schema = slocd.get(import_ns)
- if schema is None:
- schema = XMLSchema()
- slocd[import_ns] = schema
-
- try:
- tp.loadSchema(schema)
- except NoSchemaLocationWarning:
- ex = None
- del slocd[import_ns]
- continue
- except SchemaError:
- ex = None
- warnings.warn('<import namespace="%s">, %s' % (import_ns, 'failed to load schema instance, resort to lazy eval when necessary'))
- del slocd[import_ns]
-
- class _LazyEvalImport('_LazyEvalImport', (str,)):
-
- def getSchema(namespace):
- schema = slocd.get(namespace)
- if schema is None:
- parent = self._parent()
- wstypes = parent
- if isinstance(parent, WSDLToolsAdapter):
- wstypes = parent.getImportSchemas()
-
- schema = wstypes.get(namespace)
-
- if isinstance(schema, XMLSchema):
- self.imports[namespace] = schema
- return schema
-
-
-
- self.imports[import_ns] = _LazyEvalImport(import_ns)
- continue
- except:
- None<EXCEPTION MATCH>NoSchemaLocationWarning
-
-
- None<EXCEPTION MATCH>NoSchemaLocationWarning
- tp._schema = schema
- if self.getImportSchemas().has_key(import_ns):
- warnings.warn('Detected multiple imports of the namespace "%s" ' % import_ns)
-
- self.addImportSchema(schema)
- self.imports[import_ns] = tp
- continue
- if component == 'redefine':
- warnings.warn('redefine is ignored')
- continue
- if component == 'annotation':
- warnings.warn('annotation is ignored')
- continue
- if component == 'attribute':
- tp = AttributeDeclaration(self)
- tp.fromDom(childNode)
- self.attr_decl[tp.getAttribute('name')] = tp
- continue
- if component == 'attributeGroup':
- tp = AttributeGroupDefinition(self)
- tp.fromDom(childNode)
- self.attr_groups[tp.getAttribute('name')] = tp
- continue
- if component == 'element':
- tp = ElementDeclaration(self)
- tp.fromDom(childNode)
- self.elements[tp.getAttribute('name')] = tp
- continue
- if component == 'group':
- tp = ModelGroupDefinition(self)
- tp.fromDom(childNode)
- self.model_groups[tp.getAttribute('name')] = tp
- continue
- if component == 'notation':
- tp = Notation(self)
- tp.fromDom(childNode)
- self.notations[tp.getAttribute('name')] = tp
- continue
- if component == 'complexType':
- tp = ComplexType(self)
- tp.fromDom(childNode)
- self.types[tp.getAttribute('name')] = tp
- continue
- if component == 'simpleType':
- tp = SimpleType(self)
- tp.fromDom(childNode)
- self.types[tp.getAttribute('name')] = tp
- continue
-
-
-
- class Import(XMLSchemaComponent):
- attributes = {
- 'id': None,
- 'namespace': None,
- 'schemaLocation': None }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'import'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self._schema = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- if self.attributes['namespace'] == self.getTargetNamespace():
- raise SchemaError, 'namespace of schema and import match'
-
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
- def getSchema(self):
- if not self._schema:
- ns = self.attributes['namespace']
- schema = self._parent().getImportSchemas().get(ns)
- if not schema and self._parent()._parent:
- schema = self._parent()._parent().getImportSchemas().get(ns)
-
- if not schema:
- url = self.attributes.get('schemaLocation')
- if not url:
- raise SchemaError, 'namespace(%s) is unknown' % ns
-
- base_url = self._parent().getBaseUrl()
- reader = SchemaReader(base_url = base_url)
- reader._imports = self._parent().getImportSchemas()
- reader._includes = self._parent().getIncludeSchemas()
- self._schema = reader.loadFromURL(url)
-
-
- if not self._schema:
- pass
- return schema
-
-
- def loadSchema(self, schema):
- base_url = self._parent().getBaseUrl()
- reader = SchemaReader(base_url = base_url)
- reader._imports = self._parent().getImportSchemas()
- reader._includes = self._parent().getIncludeSchemas()
- self._schema = schema
- if not self.attributes.has_key('schemaLocation'):
- raise NoSchemaLocationWarning('no schemaLocation attribute in import')
-
- reader.loadFromURL(self.attributes.get('schemaLocation'), schema)
-
-
-
- class Include(XMLSchemaComponent):
- required = [
- 'schemaLocation']
- attributes = {
- 'id': None,
- 'schemaLocation': None }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'include'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self._schema = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
- def getSchema(self):
- if not self._schema:
- schema = self._parent()
- self._schema = schema.getIncludeSchemas().get(self.attributes['schemaLocation'])
- if not self._schema:
- url = self.attributes['schemaLocation']
- reader = SchemaReader(base_url = schema.getBaseUrl())
- reader._imports = schema.getImportSchemas()
- reader._includes = schema.getIncludeSchemas()
- self._schema = XMLSchema(schema)
- reader.loadFromURL(url, self._schema)
-
-
- return self._schema
-
-
-
-
- class AttributeDeclaration(XMLSchemaComponent, AttributeMarker, DeclarationMarker):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None,
- 'type': None,
- 'default': None,
- 'fixed': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleType'] }
- tag = 'attribute'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- if component == 'simpleType':
- self.content = AnonymousSimpleType(self)
- self.content.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class LocalAttributeDeclaration(AttributeDeclaration, AttributeMarker, LocalMarker, DeclarationMarker):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None,
- 'type': None,
- 'form': (lambda self: GetSchema(self).getAttributeFormDefault()),
- 'use': 'optional',
- 'default': None,
- 'fixed': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleType'] }
-
- def __init__(self, parent):
- AttributeDeclaration.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- if component == 'simpleType':
- self.content = AnonymousSimpleType(self)
- self.content.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class AttributeWildCard(XMLSchemaComponent, AttributeMarker, DeclarationMarker, WildCardMarker):
- attributes = {
- 'id': None,
- 'namespace': '##any',
- 'processContents': 'strict' }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'anyAttribute'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class AttributeReference(XMLSchemaComponent, AttributeMarker, ReferenceMarker):
- required = [
- 'ref']
- attributes = {
- 'id': None,
- 'ref': None,
- 'use': 'optional',
- 'default': None,
- 'fixed': None }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'attribute'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
-
-
- def getAttributeDeclaration(self, attribute = 'ref'):
- return XMLSchemaComponent.getAttributeDeclaration(self, attribute)
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class AttributeGroupDefinition(XMLSchemaComponent, AttributeGroupMarker, DefinitionMarker):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'attribute',
- 'attributeGroup',
- 'anyAttribute'] }
- tag = 'attributeGroup'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.attr_content = None
-
-
- def getAttributeContent(self):
- return self.attr_content
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for indx in range(len(contents)):
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'annotation' and not indx:
- self.annotation = Annotation(self)
- self.annotation.fromDom(contents[indx])
- continue
- if component == 'attribute':
- if contents[indx].hasattr('name'):
- content.append(LocalAttributeDeclaration(self))
- elif contents[indx].hasattr('ref'):
- content.append(AttributeReference(self))
- else:
- raise SchemaError, 'Unknown attribute type'
- content[-1].fromDom(contents[indx])
- continue
- if component == 'attributeGroup':
- content.append(AttributeGroupReference(self))
- content[-1].fromDom(contents[indx])
- continue
- if component == 'anyAttribute':
- if len(contents) != indx + 1:
- raise SchemaError, 'anyAttribute is out of order in %s' % self.getItemTrace()
-
- content.append(AttributeWildCard(self))
- content[-1].fromDom(contents[indx])
- continue
- raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
-
- self.attr_content = tuple(content)
-
-
-
- class AttributeGroupReference(XMLSchemaComponent, AttributeGroupMarker, ReferenceMarker):
- required = [
- 'ref']
- attributes = {
- 'id': None,
- 'ref': None }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'attributeGroup'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
-
-
- def getAttributeGroup(self, attribute = 'ref'):
- return XMLSchemaComponent.getAttributeGroup(self, attribute)
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class IdentityConstrants(XMLSchemaComponent):
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.selector = None
- self.fields = None
- self.annotation = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- fields = []
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- elif component == 'selector':
- self.selector = self.Selector(self)
- self.selector.fromDom(i)
- continue
- elif component == 'field':
- fields.append(self.Field(self))
- fields[-1].fromDom(i)
- continue
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- self.fields = tuple(fields)
-
-
-
- class Constraint(XMLSchemaComponent):
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- not (self.annotation)
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class Selector(Constraint):
- required = [
- 'xpath']
- attributes = {
- 'id': None,
- 'xpath': None }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'selector'
-
-
- class Field(Constraint):
- required = [
- 'xpath']
- attributes = {
- 'id': None,
- 'xpath': None }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'field'
-
-
-
- class Unique(IdentityConstrants):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'selector',
- 'field'] }
- tag = 'unique'
-
-
- class Key(IdentityConstrants):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'selector',
- 'field'] }
- tag = 'key'
-
-
- class KeyRef(IdentityConstrants):
- required = [
- 'name',
- 'refer']
- attributes = {
- 'id': None,
- 'name': None,
- 'refer': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'selector',
- 'field'] }
- tag = 'keyref'
-
-
- class ElementDeclaration(XMLSchemaComponent, ElementMarker, DeclarationMarker):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None,
- 'type': None,
- 'default': None,
- 'fixed': None,
- 'nillable': 0,
- 'abstract': 0,
- 'substitutionGroup': None,
- 'block': (lambda self: self._parent().getBlockDefault()),
- 'final': (lambda self: self._parent().getFinalDefault()) }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleType',
- 'complexType',
- 'key',
- 'keyref',
- 'unique'] }
- tag = 'element'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
- self.constraints = ()
-
-
- def isQualified(self):
- return True
-
-
- def getAttribute(self, attribute):
- value = XMLSchemaComponent.getAttribute(self, attribute)
- if attribute != 'type' or value is not None:
- return value
-
- if self.content is not None:
- return None
-
- parent = self
- while None:
- nsdict = parent.attributes[XMLSchemaComponent.xmlns]
- for k, v in nsdict.items():
- if v not in SCHEMA.XSD_LIST:
- continue
-
- return TypeDescriptionComponent((v, 'anyType'))
-
- if isinstance(parent, WSDLToolsAdapter) or not hasattr(parent, '_parent'):
- break
-
- parent = parent._parent()
- continue
- raise SchemaError, 'failed to locate the XSD namespace'
- return None
-
-
- def getElementDeclaration(self, attribute):
- raise Warning, 'invalid operation for <%s>' % self.tag
-
-
- def getTypeDefinition(self, attribute = None):
- if attribute:
- return XMLSchemaComponent.getTypeDefinition(self, attribute)
-
- gt = XMLSchemaComponent.getTypeDefinition(self, 'type')
- if gt:
- return gt
-
- return self.content
-
-
- def getConstraints(self):
- return self._constraints
-
-
- def setConstraints(self, constraints):
- self._constraints = tuple(constraints)
-
- constraints = property(getConstraints, setConstraints, None, 'tuple of key, keyref, unique constraints')
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- constraints = []
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- elif component == 'simpleType' and not (self.content):
- self.content = AnonymousSimpleType(self)
- self.content.fromDom(i)
- elif component == 'complexType' and not (self.content):
- self.content = LocalComplexType(self)
- self.content.fromDom(i)
- elif component == 'key':
- constraints.append(Key(self))
- constraints[-1].fromDom(i)
- elif component == 'keyref':
- constraints.append(KeyRef(self))
- constraints[-1].fromDom(i)
- elif component == 'unique':
- constraints.append(Unique(self))
- constraints[-1].fromDom(i)
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- not (self.content)
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.constraints = constraints
-
-
-
- class LocalElementDeclaration(ElementDeclaration, LocalMarker):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None,
- 'form': (lambda self: GetSchema(self).getElementFormDefault()),
- 'type': None,
- 'minOccurs': '1',
- 'maxOccurs': '1',
- 'default': None,
- 'fixed': None,
- 'nillable': 0,
- 'abstract': 0,
- 'block': (lambda self: GetSchema(self).getBlockDefault()) }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleType',
- 'complexType',
- 'key',
- 'keyref',
- 'unique'] }
-
- def isQualified(self):
- form = self.getAttribute('form')
- if form == 'qualified':
- return True
-
- if form == 'unqualified':
- return False
-
- raise SchemaError, 'Bad form (%s) for element: %s' % (form, self.getItemTrace())
-
-
-
- class ElementReference(XMLSchemaComponent, ElementMarker, ReferenceMarker):
- required = [
- 'ref']
- attributes = {
- 'id': None,
- 'ref': None,
- 'minOccurs': '1',
- 'maxOccurs': '1' }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'element'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
-
-
- def getElementDeclaration(self, attribute = None):
- if attribute:
- return XMLSchemaComponent.getElementDeclaration(self, attribute)
-
- return XMLSchemaComponent.getElementDeclaration(self, 'ref')
-
-
- def fromDom(self, node):
- self.annotation = None
- self.setAttributes(node)
- for i in self.getContents(node):
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- not (self.annotation)
-
-
-
-
- class ElementWildCard(LocalElementDeclaration, WildCardMarker):
- required = []
- attributes = {
- 'id': None,
- 'minOccurs': '1',
- 'maxOccurs': '1',
- 'namespace': '##any',
- 'processContents': 'strict' }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'any'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
-
-
- def isQualified(self):
- return GetSchema(self).isElementFormDefaultQualified()
-
-
- def getAttribute(self, attribute):
- return XMLSchemaComponent.getAttribute(self, attribute)
-
-
- def getTypeDefinition(self, attribute):
- raise Warning, 'invalid operation for <%s>' % self.tag
-
-
- def fromDom(self, node):
- self.annotation = None
- self.setAttributes(node)
- for i in self.getContents(node):
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- not (self.annotation)
-
-
-
-
- class Sequence(XMLSchemaComponent, SequenceMarker):
- attributes = {
- 'id': None,
- 'minOccurs': '1',
- 'maxOccurs': '1' }
- contents = {
- 'xsd': [
- 'annotation',
- 'element',
- 'group',
- 'choice',
- 'sequence',
- 'any'] }
- tag = 'sequence'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- elif component == 'element':
- if i.hasattr('ref'):
- content.append(ElementReference(self))
- else:
- content.append(LocalElementDeclaration(self))
- elif component == 'group':
- content.append(ModelGroupReference(self))
- elif component == 'choice':
- content.append(Choice(self))
- elif component == 'sequence':
- content.append(Sequence(self))
- elif component == 'any':
- content.append(ElementWildCard(self))
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- content[-1].fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.content = tuple(content)
-
-
-
- class All(XMLSchemaComponent, AllMarker):
- attributes = {
- 'id': None,
- 'minOccurs': '1',
- 'maxOccurs': '1' }
- contents = {
- 'xsd': [
- 'annotation',
- 'element'] }
- tag = 'all'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- elif component == 'element':
- if i.hasattr('ref'):
- content.append(ElementReference(self))
- else:
- content.append(LocalElementDeclaration(self))
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- content[-1].fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.content = tuple(content)
-
-
-
- class Choice(XMLSchemaComponent, ChoiceMarker):
- attributes = {
- 'id': None,
- 'minOccurs': '1',
- 'maxOccurs': '1' }
- contents = {
- 'xsd': [
- 'annotation',
- 'element',
- 'group',
- 'choice',
- 'sequence',
- 'any'] }
- tag = 'choice'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- elif component == 'element':
- if i.hasattr('ref'):
- content.append(ElementReference(self))
- else:
- content.append(LocalElementDeclaration(self))
- elif component == 'group':
- content.append(ModelGroupReference(self))
- elif component == 'choice':
- content.append(Choice(self))
- elif component == 'sequence':
- content.append(Sequence(self))
- elif component == 'any':
- content.append(ElementWildCard(self))
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- content[-1].fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.content = tuple(content)
-
-
-
- class ModelGroupDefinition(XMLSchemaComponent, ModelGroupMarker, DefinitionMarker):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'all',
- 'choice',
- 'sequence'] }
- tag = 'group'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- elif component == 'all' and not (self.content):
- self.content = All(self)
- elif component == 'choice' and not (self.content):
- self.content = Choice(self)
- elif component == 'sequence' and not (self.content):
- self.content = Sequence(self)
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- self.content.fromDom(i)
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class ModelGroupReference(XMLSchemaComponent, ModelGroupMarker, ReferenceMarker):
- required = [
- 'ref']
- attributes = {
- 'id': None,
- 'ref': None,
- 'minOccurs': '1',
- 'maxOccurs': '1' }
- contents = {
- 'xsd': [
- 'annotation'] }
- tag = 'group'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
-
-
- def getModelGroupReference(self):
- return self.getModelGroup('ref')
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- not (self.annotation)
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
- class ComplexType(XMLSchemaComponent, DefinitionMarker, ComplexMarker):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None,
- 'mixed': 0,
- 'abstract': 0,
- 'block': (lambda self: self._parent().getBlockDefault()),
- 'final': (lambda self: self._parent().getFinalDefault()) }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleContent',
- 'complexContent',
- 'group',
- 'all',
- 'choice',
- 'sequence',
- 'attribute',
- 'attributeGroup',
- 'anyAttribute',
- 'any'] }
- tag = 'complexType'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
- self.attr_content = None
-
-
- def isMixed(self):
- m = self.getAttribute('mixed')
- if m == 0 or m == False:
- return False
-
- if isinstance(m, basestring) is True:
- if m in ('false', '0'):
- return False
-
- if m in ('true', '1'):
- return True
-
-
- raise SchemaError, 'invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace())
-
-
- def getAttributeContent(self):
- return self.attr_content
-
-
- def getElementDeclaration(self, attribute):
- raise Warning, 'invalid operation for <%s>' % self.tag
-
-
- def getTypeDefinition(self, attribute):
- raise Warning, 'invalid operation for <%s>' % self.tag
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- indx = 0
- num = len(contents)
- if not num:
- return None
-
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'annotation':
- self.annotation = Annotation(self)
- self.annotation.fromDom(contents[indx])
- indx += 1
- component = SplitQName(contents[indx].getTagName())[1]
-
- self.content = None
- if component == 'simpleContent':
- self.content = self.__class__.SimpleContent(self)
- self.content.fromDom(contents[indx])
- elif component == 'complexContent':
- self.content = self.__class__.ComplexContent(self)
- self.content.fromDom(contents[indx])
- elif component == 'all':
- self.content = All(self)
- elif component == 'choice':
- self.content = Choice(self)
- elif component == 'sequence':
- self.content = Sequence(self)
- elif component == 'group':
- self.content = ModelGroupReference(self)
-
- if self.content:
- self.content.fromDom(contents[indx])
- indx += 1
-
- self.attr_content = []
- while indx < num:
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'attribute':
- if contents[indx].hasattr('ref'):
- self.attr_content.append(AttributeReference(self))
- else:
- self.attr_content.append(LocalAttributeDeclaration(self))
- elif component == 'attributeGroup':
- self.attr_content.append(AttributeGroupReference(self))
- elif component == 'anyAttribute':
- self.attr_content.append(AttributeWildCard(self))
- else:
- raise SchemaError, 'Unknown component (%s): %s' % (contents[indx].getTagName(), self.getItemTrace())
- self.attr_content[-1].fromDom(contents[indx])
- indx += 1
-
-
- class _DerivedType(XMLSchemaComponent):
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.derivation = None
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for i in contents:
- component = SplitQName(i.getTagName())[1]
- if component in self.__class__.contents['xsd']:
- if component == 'annotation' and not (self.annotation):
- self.annotation = Annotation(self)
- self.annotation.fromDom(i)
- continue
- elif component == 'restriction' and not (self.derivation):
- self.derivation = self.__class__.Restriction(self)
- elif component == 'extension' and not (self.derivation):
- self.derivation = self.__class__.Extension(self)
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- else:
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
- self.derivation.fromDom(i)
-
- self.content = self.derivation
-
-
-
- class ComplexContent(_DerivedType, ComplexMarker):
- attributes = {
- 'id': None,
- 'mixed': 0 }
- contents = {
- 'xsd': [
- 'annotation',
- 'restriction',
- 'extension'] }
- tag = 'complexContent'
-
- def isMixed(self):
- m = self.getAttribute('mixed')
- if m == 0 or m == False:
- return False
-
- if isinstance(m, basestring) is True:
- if m in ('false', '0'):
- return False
-
- if m in ('true', '1'):
- return True
-
-
- raise SchemaError, 'invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace())
-
-
- class _DerivationBase(XMLSchemaComponent):
- required = [
- 'base']
- attributes = {
- 'id': None,
- 'base': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'group',
- 'all',
- 'choice',
- 'sequence',
- 'attribute',
- 'attributeGroup',
- 'anyAttribute'] }
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
- self.attr_content = None
-
-
- def getAttributeContent(self):
- return self.attr_content
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- indx = 0
- num = len(contents)
- if not num:
- return None
-
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'annotation':
- self.annotation = Annotation(self)
- self.annotation.fromDom(contents[indx])
- indx += 1
- component = SplitQName(contents[indx].getTagName())[1]
-
- if component == 'all':
- self.content = All(self)
- self.content.fromDom(contents[indx])
- indx += 1
- elif component == 'choice':
- self.content = Choice(self)
- self.content.fromDom(contents[indx])
- indx += 1
- elif component == 'sequence':
- self.content = Sequence(self)
- self.content.fromDom(contents[indx])
- indx += 1
- elif component == 'group':
- self.content = ModelGroupReference(self)
- self.content.fromDom(contents[indx])
- indx += 1
- else:
- self.content = None
- self.attr_content = []
- while indx < num:
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'attribute':
- if contents[indx].hasattr('ref'):
- self.attr_content.append(AttributeReference(self))
- else:
- self.attr_content.append(LocalAttributeDeclaration(self))
- elif component == 'attributeGroup':
- if contents[indx].hasattr('ref'):
- self.attr_content.append(AttributeGroupReference(self))
- else:
- self.attr_content.append(AttributeGroupDefinition(self))
- elif component == 'anyAttribute':
- self.attr_content.append(AttributeWildCard(self))
- else:
- raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
- self.attr_content[-1].fromDom(contents[indx])
- indx += 1
-
-
-
- class Extension(_DerivationBase, ExtensionMarker):
- tag = 'extension'
-
-
- class Restriction(_DerivationBase, RestrictionMarker):
- tag = 'restriction'
-
-
-
- class SimpleContent(_DerivedType, SimpleMarker):
- attributes = {
- 'id': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'restriction',
- 'extension'] }
- tag = 'simpleContent'
-
- class Extension(XMLSchemaComponent, ExtensionMarker):
- required = [
- 'base']
- attributes = {
- 'id': None,
- 'base': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'attribute',
- 'attributeGroup',
- 'anyAttribute'] }
- tag = 'extension'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.attr_content = None
-
-
- def getAttributeContent(self):
- return self.attr_content
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- indx = 0
- num = len(contents)
- if num:
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'annotation':
- self.annotation = Annotation(self)
- self.annotation.fromDom(contents[indx])
- indx += 1
- component = SplitQName(contents[indx].getTagName())[1]
-
-
- content = []
- while indx < num:
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'attribute':
- if contents[indx].hasattr('ref'):
- content.append(AttributeReference(self))
- else:
- content.append(LocalAttributeDeclaration(self))
- elif component == 'attributeGroup':
- content.append(AttributeGroupReference(self))
- elif component == 'anyAttribute':
- content.append(AttributeWildCard(self))
- else:
- raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
- content[-1].fromDom(contents[indx])
- indx += 1
- self.attr_content = tuple(content)
-
-
-
- class Restriction(XMLSchemaComponent, RestrictionMarker):
- required = [
- 'base']
- attributes = {
- 'id': None,
- 'base': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleType',
- 'attribute',
- 'attributeGroup',
- 'anyAttribute'] + RestrictionMarker.facets }
- tag = 'restriction'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
- self.attr_content = None
-
-
- def getAttributeContent(self):
- return self.attr_content
-
-
- def fromDom(self, node):
- self.content = []
- self.setAttributes(node)
- contents = self.getContents(node)
- indx = 0
- num = len(contents)
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'annotation':
- self.annotation = Annotation(self)
- self.annotation.fromDom(contents[indx])
- indx += 1
- component = SplitQName(contents[indx].getTagName())[1]
-
- content = []
- while indx < num:
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'attribute':
- if contents[indx].hasattr('ref'):
- content.append(AttributeReference(self))
- else:
- content.append(LocalAttributeDeclaration(self))
- elif component == 'attributeGroup':
- content.append(AttributeGroupReference(self))
- elif component == 'anyAttribute':
- content.append(AttributeWildCard(self))
- elif component == 'simpleType':
- self.content.append(AnonymousSimpleType(self))
- self.content[-1].fromDom(contents[indx])
- else:
- raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
- content[-1].fromDom(contents[indx])
- indx += 1
- self.attr_content = tuple(content)
-
-
-
-
-
- class LocalComplexType(ComplexType, LocalMarker):
- required = []
- attributes = {
- 'id': None,
- 'mixed': 0 }
- tag = 'complexType'
-
-
- class SimpleType(XMLSchemaComponent, DefinitionMarker, SimpleMarker):
- required = [
- 'name']
- attributes = {
- 'id': None,
- 'name': None,
- 'final': (lambda self: self._parent().getFinalDefault()) }
- contents = {
- 'xsd': [
- 'annotation',
- 'restriction',
- 'list',
- 'union'] }
- tag = 'simpleType'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def getElementDeclaration(self, attribute):
- raise Warning, 'invalid operation for <%s>' % self.tag
-
-
- def getTypeDefinition(self, attribute):
- raise Warning, 'invalid operation for <%s>' % self.tag
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- for child in contents:
- component = SplitQName(child.getTagName())[1]
- if component == 'annotation':
- self.annotation = Annotation(self)
- self.annotation.fromDom(child)
- continue
-
- else:
- return None
- if component == 'restriction':
- self.content = self.__class__.Restriction(self)
- elif component == 'list':
- self.content = self.__class__.List(self)
- elif component == 'union':
- self.content = self.__class__.Union(self)
- else:
- raise SchemaError, 'Unknown component (%s)' % component
- self.content.fromDom(child)
-
-
- class Restriction(XMLSchemaComponent, RestrictionMarker):
- attributes = {
- 'id': None,
- 'base': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleType'] + RestrictionMarker.facets }
- tag = 'restriction'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
- self.facets = None
-
-
- def getAttributeBase(self):
- return XMLSchemaComponent.getAttribute(self, 'base')
-
-
- def getTypeDefinition(self, attribute = 'base'):
- return XMLSchemaComponent.getTypeDefinition(self, attribute)
-
-
- def getSimpleTypeContent(self):
- for el in self.content:
- if el.isSimple():
- return el
- continue
-
-
-
- def fromDom(self, node):
- self.facets = []
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for indx in range(len(contents)):
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'annotation' and not indx:
- self.annotation = Annotation(self)
- self.annotation.fromDom(contents[indx])
- continue
- continue
- if component == 'simpleType':
- if not indx or indx == 1:
- content.append(AnonymousSimpleType(self))
- content[-1].fromDom(contents[indx])
- continue
- if component in RestrictionMarker.facets:
- self.facets.append(contents[indx])
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.content = tuple(content)
-
-
-
- class Union(XMLSchemaComponent, UnionMarker):
- attributes = {
- 'id': None,
- 'memberTypes': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleType'] }
- tag = 'union'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def fromDom(self, node):
- self.setAttributes(node)
- contents = self.getContents(node)
- content = []
- for indx in range(len(contents)):
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'annotation' and not indx:
- self.annotation = Annotation(self)
- self.annotation.fromDom(contents[indx])
- continue
- if component == 'simpleType':
- content.append(AnonymousSimpleType(self))
- content[-1].fromDom(contents[indx])
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
- self.content = tuple(content)
-
-
-
- class List(XMLSchemaComponent, ListMarker):
- attributes = {
- 'id': None,
- 'itemType': None }
- contents = {
- 'xsd': [
- 'annotation',
- 'simpleType'] }
- tag = 'list'
-
- def __init__(self, parent):
- XMLSchemaComponent.__init__(self, parent)
- self.annotation = None
- self.content = None
-
-
- def getItemType(self):
- return self.attributes.get('itemType')
-
-
- def getTypeDefinition(self, attribute = 'itemType'):
- tp = XMLSchemaComponent.getTypeDefinition(self, attribute)
- if not tp:
- pass
- return self.content
-
-
- def fromDom(self, node):
- self.annotation = None
- self.content = None
- self.setAttributes(node)
- contents = self.getContents(node)
- for indx in range(len(contents)):
- component = SplitQName(contents[indx].getTagName())[1]
- if component == 'annotation' and not indx:
- self.annotation = Annotation(self)
- self.annotation.fromDom(contents[indx])
- continue
- if component == 'simpleType':
- self.content = AnonymousSimpleType(self)
- self.content.fromDom(contents[indx])
- break
- continue
- raise SchemaError, 'Unknown component (%s)' % i.getTagName()
-
-
-
-
-
- class AnonymousSimpleType(SimpleType, SimpleMarker, LocalMarker):
- required = []
- attributes = {
- 'id': None }
- tag = 'simpleType'
-
-
- class Redefine:
- tag = 'redefine'
-
- if sys.version_info[:2] >= (2, 2):
- tupleClass = tuple
- else:
- import UserTuple
- tupleClass = UserTuple.UserTuple
-
- class TypeDescriptionComponent(tupleClass):
-
- def __init__(self, args):
- if len(args) != 2:
- raise TypeError, 'expecting tuple (namespace, name), got %s' % args
- elif args[1].find(':') >= 0:
- args = (args[0], SplitQName(args[1])[1])
-
- tuple.__init__(self, args)
-
-
- def getTargetNamespace(self):
- return self[0]
-
-
- def getName(self):
- return self[1]
-
-
-